var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      nickName: '未登录',
      avatarUrl: '../imgs/demoheadicon.png'
    },
    description:'',
    duser:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      wait_return: 0,
      wait_pay: 0,
      wait_receive: 0,
      wait_comment: 0
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.ready(() => {
      this.loadUserinfo();
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  myItemsTap: function (event) {
    var itemtype = event.currentTarget.dataset.itemtype;
    console.log(itemtype);
    switch (itemtype) {
      case 'car':
        wx.navigateTo({
          url: './car/car',
        });
        break;
      case 'order':
        wx.navigateTo({
          url: '../orderlist/orderlist',
        });
        break;
      case 'address':
        wx.navigateTo({
          url: './address/address',
        });
        break;
      case 'coupon':
        wx.navigateTo({
          url: './coupon/coupon',
        });
        break;
      case 'setting':
        wx.navigateTo({
          url: './setting/setting',
        });
        break;
      case 'collect':
        wx.navigateTo({
          url: './collect/collect',
        });
        break;
      case 'make':
        wx.navigateTo({
          url: '../custom/makelist/makelist',
        });
        break;
      case 'money':
        if (app.globalData.duserID) {
          wx.navigateTo({
            url: './money/money',
          });
        }
        break;
      case 'inout':
        wx.navigateTo({
          url: './inout/inout',
        });
        break;
      case 'clients':
        wx.navigateTo({
          url: './clients/clients',
        });
        break;
      case 'qrcode':
        if (app.globalData.duserID){
          var pathUrl = './qrcode/qrcode?buyqr=' + this.data.duser.buyqr + '&addqr=' + this.data.duser.addqr;
          wx.navigateTo({
            url: pathUrl,
          });
        }        
        break;
      case 'scan':
        wx.scanCode({
          success:function(res){
            var path = '../custom/makeself/makeself?' + res.result
            wx.navigateTo({
              url: path,
            });
          }
        });
        break;
      case 'news':
        wx.navigateTo({
          url: '../news/collectlist',
        });
        break;
    }
  },
  goToOrderlist: function (event) {
    var ordertype = event.currentTarget.dataset.ordertype;
    var desc = event.currentTarget.dataset.orderdesc;
    var url = '../orderlist/orderlist2?ordertype=' + ordertype + '&desc=' + desc;
    if (ordertype == '1') {
      url = '../orderlist/orderlist?ordertype=' + ordertype + '&desc=' + desc;
    }
    else if(ordertype == 'RETURN'){
      url = '../orderlist/returnlist';
    }
    else if (ordertype == 'WAITCCOMMENT'){
      url = '../orderlist/commentlist/commentlist';
    }
    wx.navigateTo({
      url: url,
    });
  },
  loadUserinfo:function(){
    var url = '/index.php/api/User/index';
    app.request(url).then(this.loadUserinfoCallBack.bind(this))
  },
  loadUserinfoCallBack:function(res){
    console.log(res);
    if (res.status != 'ok' || res.result.user == null){
      return;
    }
    // return_count 退换货数量  waitPay 待支付数量  waitSend 待发货数量  waitReceive 待收货数量 uncomment_count 带评论数量
    this.setData({
      wait_return: res.result.user.return_count,
      wait_pay: res.result.user.waitPay,
      wait_receive: res.result.user.waitReceive,
      wait_comment: res.result.user.uncomment_count
    });

    if (res.result.user.examine){
      var states = res.result.user.examine.states;
      var description = '';
      if (states == 1){
        description = '（分销商身份审核中）'
      }
      else if(states == 2){
        description = '（分销商身份审核出错：' + res.result.user.examine.error + '）'
      }
      else if(states == 3){
        description = '（分销商身份审核成功）'
      }
      this.setData({
        description: description
      });
    }

    if (res.result.user.duser) {
      app.globalData.duserID = res.result.user.duser.id;
      app.globalData.totalMoney = res.result.user.duser.totalmoney;
      app.globalData.cangetMoney = res.result.cangetMoney;
      app.globalData.nickname = res.result.user.nickname;
      app.globalData.headpic = res.result.user.head_pic;
      
      var roleNameObj = {};
      var tmpArr = res.result.user.dlevel;
      for(var i=0;i<tmpArr.length;i++){
        roleNameObj[tmpArr[i].level_id] = tmpArr[i].level_name;
      }
      this.setData({
        duser: res.result.user.duser,
        levelName: roleNameObj
      });
    }
  }
})